package com.controller;

import com.comment.CommentResult;
import com.domain.entity.Goods;
import com.domain.req.AddGoodsReq;
import com.domain.req.PageListReq;
import com.domain.req.PageReq;
import com.domain.req.UpdateGoodsReq;
import com.domain.vo.GoodsGetByIdVO;
import com.github.pagehelper.PageInfo;
import com.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin//跨越
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增商品
     */
    @PostMapping("/addGoods")
    public CommentResult<Void> addGoods(@RequestBody AddGoodsReq addGoodsReq) {
        goodsService.addGoods(addGoodsReq);
        return CommentResult.success();
    }

    /**
     * 根据id查询商品信息
     */
    @GetMapping("/getById/{id}")
    public CommentResult<GoodsGetByIdVO> getById(@PathVariable("id") Integer id) {
        GoodsGetByIdVO goodsGetByIdVO = goodsService.getById(id);
        return CommentResult.success(goodsGetByIdVO);
    }

    /**
     * 分页查询商品信息
     */
    @GetMapping("/pageList")
    public CommentResult<PageInfo<Goods>> pageList(PageReq pageReq, PageListReq pageListReq) {
        PageInfo<Goods> pageInfo = goodsService.pageList(pageReq, pageListReq);
        return CommentResult.success(pageInfo);
    }

    /**
     * 根据商品id删除商品信息
     */
    @DeleteMapping("/deleteById/{id}")
    public CommentResult<Void> deleteById(@PathVariable("id") Integer id) {
        goodsService.deleteById(id);
        return CommentResult.success();
    }

    /**
     * 根据商品id修改商品信息
     */
    @PutMapping("/updateById")
    public CommentResult<Void> updateById(@RequestBody UpdateGoodsReq updateGoodsReq) {
        goodsService.updateById(updateGoodsReq);
        return CommentResult.success();
    }

    @PutMapping("/update")
    public CommentResult<Void> update(@RequestBody Goods goods) {
        goodsService.update(goods);
        return CommentResult.success();
    }

    @GetMapping("/selectGoods")
    public CommentResult<List<Goods>> selectGoods(String searchValue) {
        List<Goods> goodsList = goodsService.selectGoods(searchValue);
        return CommentResult.success(goodsList);
    }


    //添加
//    @RequestMapping(value = "addGoods")
//    public CommentResult<Void> addGoods(Goods goods){
//        log.info("-----------addGoods--------------");
//        goodsMapper.insertGoods(goods);
//        return CommentResult.success();
//    }
//
//    //查询
//    @RequestMapping(value = "querygoods")
//    public CommentResult<PageInfo<Goods>> querygoods(Goods goods,int pageNum,int pageSize){
//        log.info("-----------querygoods--------------");
//        System.out.println("pageNum   "+pageNum);
//        System.out.println("pageSize   "+pageSize);
//        //初始化
//        PageHelper.startPage(pageNum,pageSize);
//        //原查询
//        List<Goods> goodsList=goodsMapper.selectGoods();
//        //格式转换
//        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
//        return CommentResult.success(goodsPageInfo);
//    }
//
//    //根据id查询
//    @RequestMapping(value = "querygoods1")
//    public CommentResult<List<Goods>> querygoods1(Goods goods){
//        log.info("-----------querygoods1--------------");
//        return CommentResult.success(goodsMapper.selectGoodsById(goods));
//    }
//
//    //删除
//    @RequestMapping(value = "/delGoods/{id}", method = RequestMethod.DELETE)
//    public CommentResult<Void> delGoods(@PathVariable("id") Long id){
//        log.info("-----------delGoods--------------");
//        goodsMapper.delGoods(id);
//        return CommentResult.success();
//    }
//
//    //根据id修改信息
//    @RequestMapping(value = "updateGoods", method = RequestMethod.PUT)
//    public CommentResult<Void> updateGoods(@RequestBody Goods goods){
//        log.info("-----------updateGoods--------------");
//        // 补充修改时间
//        goods.setUpdateTime(new Date());
//        goodsMapper.updateGoods(goods);
//        return CommentResult.success();
//    }
}


